/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pro.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pro.entity.Folder;
import com.thinkgem.jeesite.modules.pro.entity.Project;
import com.thinkgem.jeesite.modules.pro.service.FolderService;
import com.thinkgem.jeesite.modules.pro.service.ProjectService;

/**
 * 文件夹管理Controller
 * @author fanqinglong
 * @version 2017-05-03
 */
@Controller
@RequestMapping(value = "${adminPath}/pro/folder")
public class FolderController extends BaseController {

	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private FolderService folderService;
	
	@ModelAttribute
	public Folder get(@RequestParam(required=false) String id) {
		Folder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = folderService.get(id);
		}
		if (entity == null){
			entity = new Folder();
		}
		return entity;
	}
	
	@RequiresPermissions("pro:folder:view")
	@RequestMapping(value = {"list", ""})
	public String list(Folder folder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Folder> page = folderService.findPage(new Page<Folder>(request, response), folder); 
		model.addAttribute("page", page);
		model.addAttribute("folder", folder);
		return "modules/pro/folderList";
	}
	
	@RequiresPermissions("pro:folder:view")
	@RequestMapping(value = {"index"})
	public String index(){
		return "modules/pro/folderIndex";
	}
	
	@RequestMapping(value = {"projectTree"})
	public String projectTree(Model model){
		model.addAttribute("tree", projectService.findList(new Project()));
		return "modules/pro/projectTreeFolder";
	}

	@RequiresPermissions("pro:folder:view")
	@RequestMapping(value = "form")
	public String form(Folder folder, Model model) {
		model.addAttribute("folder", folder);
		return "modules/pro/folderForm";
	}

	@RequiresPermissions("pro:folder:edit")
	@RequestMapping(value = "save")
	public String save(Folder folder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, folder)){
			return form(folder, model);
		}
		folderService.save(folder);
		addMessage(redirectAttributes, "保存文件夹成功");
		return "redirect:"+Global.getAdminPath()+"/pro/folder/?repage&project.id="+folder.getProject().getId();
	}
	
	@RequiresPermissions("pro:folder:edit")
	@RequestMapping(value = "delete")
	public String delete(Folder folder, RedirectAttributes redirectAttributes) {
		folderService.delete(folder);
		addMessage(redirectAttributes, "删除文件夹成功");
		return "redirect:"+Global.getAdminPath()+"/pro/folder/?repage&project.id="+folder.getProject().getId();
	}

}